//
//  BaseNavCtrl.m
//  DogeLegs
//
//  Created by 宋华 on 2017/8/15.
//  Copyright © 2017年 宋华. All rights reserved.
//

#import "BaseNavCtrl.h"
#import "BaseSubViewCtrl.h"

@interface BaseNavCtrl ()

@end

@implementation BaseNavCtrl

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBar.translucent = NO;
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 重写push、pop方法
- (void)popself {
    [self popViewControllerAnimated:YES];
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    [super pushViewController:viewController animated:animated];
    
    if (viewController.navigationItem.leftBarButtonItem == nil && [self.viewControllers count] > 1)
    {
        UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];

        UIImage *image = [UIImage imageNamed:@"back"];
        
        CGSize imageSize = image.size;
        [leftBtn setImage:image forState:UIControlStateNormal];
        [leftBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0-imageSize.width, 0, 0)];
        [leftBtn setFrame:CGRectMake(0, 0, imageSize.width, imageSize.height)];
        
        [leftBtn addTarget:self action:@selector(popself) forControlEvents:UIControlEventTouchUpInside];
        
        viewController.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    }
}

// 重写视图控制器退出方法
- (UIViewController *)popViewControllerAnimated:(BOOL)animated; // Returns the popped controller.
{
    UIViewController *controller=[super popViewControllerAnimated:animated];
    
    [self cancelAllRequest:(BaseSubViewCtrl *)controller];
    
    return controller;
}

#pragma mark - 拦截导航栏方法进行扩展
- (void)popToViewControllerClass:(Class)_class animated:(BOOL)animate {
    UIViewController *viewCtrl = nil;
    for (NSInteger i = self.viewControllers.count - 1; i >= 0 ; i--) {
        UIViewController *realCtrl = [self.viewControllers objectAtIndex:i];
        if ([realCtrl isMemberOfClass:_class]) {
            viewCtrl = realCtrl;
            break;
        }
    }
    [self popToViewController:viewCtrl animated:animate];
}

- (NSArray *)popToViewController:(UIViewController *)_ctrl animated:(BOOL)animate {
    NSArray *array = [super popToViewController:_ctrl animated:animate];
    for (NSInteger i = 0; i < array.count; i++) {
        [self cancelAllRequest:(BaseSubViewCtrl *)[array objectAtIndex:i]];
    }
    return array;
}

- (void)cancelAllRequest:(BaseSubViewCtrl *)_baseViewCtrl {
    if ([_baseViewCtrl isKindOfClass:[BaseSubViewCtrl class]])
    {
        BaseSubViewCtrl *tempController = (BaseSubViewCtrl *)_baseViewCtrl;
        
        // 取消退出视图控制器的网络请求
        for (NSURLSessionDataTask *dataTask in tempController.taskArr) {
            
            if (dataTask.state == NSURLSessionTaskStateRunning || dataTask.state == NSURLSessionTaskStateSuspended) {
                [dataTask cancel];
            }
        }
    }
}

- (void)popViewContrioller
{
    [self popViewControllerAnimated:YES];
}

@end
